package com.gooapple.notepad.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.gooapple.notepad.R;
import com.gooapple.notepad.basic.BasicActivity;
import com.gooapple.notepad.constant.Constant;
import com.gooapple.notepad.model.HomeInfo;
import com.gooapple.notepad.model.SongInfo;
import com.gooapple.notepad.service.AudioPlayerService;
import com.gooapple.notepad.util.HomeListAdapter;
import com.gooapple.notepad.util.MediaHelper;
import com.gooapple.notepad.util.UIManager;

public class HomeActivity extends BasicActivity {
	private HomeListAdapter homeListAdapter;
	private List<HomeInfo> homeInfos;
	private ListView homeListVew;
	private String[] homeTextArray;
	private String[] homeTextDetailArray;

	private BroadcastReceiver songInfoReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i("SongChange", "_____________________");
			updateNowPlayingInfos();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initViews();
		initData();
		setListeners();
	}

	@Override
	protected void initData() {
		homeTextArray = getResources().getStringArray(
				R.array.home_list_info_text_array);
		homeTextDetailArray = getResources().getStringArray(
				R.array.home_list_info_text_details_array);
		homeInfos = new ArrayList<HomeInfo>();
		HomeInfo homeInfo = null;

		for (int i = 0; i < homeTextArray.length; i++) {
			homeInfo = new HomeInfo(homeTextArray[i], homeTextDetailArray[i]);
			homeInfos.add(homeInfo);
		}
		homeListAdapter = new HomeListAdapter(homeInfos, this);
		homeListVew.setAdapter(homeListAdapter);
		/* register receiver to receive song information */
		IntentFilter filter = new IntentFilter(Constant.ACTION_SONG_CHANGED);
		registerReceiver(songInfoReceiver, filter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		HomeInfo homeInfo = null;
		// set data for audio,picture and now playing
		/*SongInfo nowPlaying = AudioPlayerService.getCurrentSongInfo(this);
		if (nowPlaying != null) {
			homeInfos.remove(Constant.POS_NOW_PLAYING);
			homeInfo = new HomeInfo(homeTextArray[6], nowPlaying.getTilte());
			homeInfos.add(6, homeInfo);
		}*/
		updateNowPlayingInfos();
		int totalSongCount = AudioPlayerService.getSongCount(this);
		homeInfos.remove(Constant.POS_AUDIO);
		homeInfo = new HomeInfo(
				homeTextArray[7],
				(totalSongCount + ((totalSongCount == 1) ? "  Song" : "  Songs")));
		homeInfos.add(7, homeInfo);
		// set pictures data
		int pictureCount = MediaHelper.getPictureCount(this);
		homeInfos.remove(Constant.POS_PICTURES);
		homeInfo = new HomeInfo(homeTextArray[8],
				(pictureCount + ((pictureCount == 1) ? "  Pictures"
						: "  Pictures")));
		homeInfos.add(8, homeInfo);
		homeListAdapter.setHomeInfos(homeInfos);
		homeListAdapter.notifyDataSetChanged();
	}

	private void updateNowPlayingInfos() {
		HomeInfo homeInfo = null;
		// set data for audio,picture and now playing
		SongInfo nowPlaying = AudioPlayerService.getCurrentSongInfo(this);
		if (nowPlaying != null) {
			homeInfos.remove(Constant.POS_NOW_PLAYING);
			homeInfo = new HomeInfo(homeTextArray[6], nowPlaying.getTilte());
			homeInfos.add(6, homeInfo);
		}
		homeListAdapter.setHomeInfos(homeInfos);
		homeListAdapter.notifyDataSetChanged();
	}

	@Override
	protected void initViews() {
		setContentView(R.layout.home);
		homeListVew = (ListView) findViewById(R.id.home_list);
		UIManager.showBasicBar(this, R.drawable.icon_home_reversed,
				getResources().getText(R.string.home_top_text), null, true,
				true);
	}

	@Override
	protected void setListeners() {
		homeListVew.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			}
		});
		homeListVew.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				ImageView iconView = null;
				int count = parent.getChildCount();
				Drawable focusD = getResources().getDrawable(
						R.drawable.home_list_deliver_focused);
				Drawable defaultD = getResources().getDrawable(
						R.drawable.home_list_deliver_normal);
				for (int i = 0; i < count; i++) {
					View v = parent.getChildAt(i);
					v.setBackgroundDrawable(defaultD);
					iconView = (ImageView) v.findViewById(R.id.home_info_icon);
					iconView.setVisibility(View.GONE);
				}
				iconView = (ImageView) view.findViewById(R.id.home_info_icon);
				iconView.setVisibility(View.VISIBLE);
				view.setBackgroundDrawable(focusD);
				setHomeListIcons(position, iconView);
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
		homeListVew.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ImageView iconView = (ImageView) view
						.findViewById(R.id.home_info_icon);
				setHomeListIcons(position, iconView);
				Intent intent = null;
				switch (position) {
				case Constant.POS_CONTINUE_READING:
					break;
				case Constant.POS_BOOKS_BY_TITLE:
					break;
				case Constant.POS_BOOKS_BY_AUTHOR:
					break;
				case Constant.POS_BOOKS_BY_DATE:
					break;
				case Constant.POS_COLLECTIONS:
					break;
				case Constant.POS_ALL_BOOKMARKS:
					break;
				case Constant.POS_NOW_PLAYING:
					break;
				case Constant.POS_AUDIO:
					intent = new Intent(Constant.ACTION_SONG_LIST);
					break;
				case Constant.POS_PICTURES:
					intent = new Intent(Constant.ACTION_PICTURE_LIST);
					break;
				case Constant.POS_SETTINGS:
					break;
				}
				if (intent != null) {
					startActivity(intent);
				}
			}
		});
	}

	private void setHomeListIcons(int position, ImageView iconView) {
		switch (position) {
		case Constant.POS_CONTINUE_READING:
			iconView.setImageResource(R.drawable.icon_continue_read);
			break;
		case Constant.POS_BOOKS_BY_TITLE:
			iconView.setImageResource(R.drawable.icon_books_by_title);
			break;
		case Constant.POS_BOOKS_BY_AUTHOR:
			iconView.setImageResource(R.drawable.icon_books_by_author);
			break;
		case Constant.POS_BOOKS_BY_DATE:
			iconView.setImageResource(R.drawable.icon_books_by_date);
			break;
		case Constant.POS_AUDIO:
			iconView.setImageResource(R.drawable.icon_audio);
			break;
		case Constant.POS_PICTURES:
			iconView.setImageResource(R.drawable.icon_picture);
			break;
		}
	}

}
